﻿/*
* 创建者：LIYINFAI
* 创建时间：2023/10/15 11:40:00
* 描述：
* --------------------------------------
* 修改说明：
* --------------------------------------
*/
using CommonLibrary.Forms;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace CommonLibrary
{
    /// <summary>
    /// 出站信息管理窗体
    /// </summary>
    public partial class FormProductOut2 : HWFrmWithTitle
    {
        private MultiPLCStationTag mPLCStationTag = MultiPLCStationTag.Load();
        /// <summary>
        /// 构造函数
        /// </summary>
        public FormProductOut2()
        {
            InitializeComponent();
        }
        private void FormProductOut_Load(object sender, EventArgs e)
        {
            InitColumns();
            LoadProductOuts();
        }
        private void InitSubData(MultiPLCTagParameter multiTags)
        {
            this.tabControl.TabPages.Clear();
            if (multiTags != null)
            {
                int pageIndex = 1;
                foreach (var m in multiTags.Parameters)
                {
                    TabPage tabPage = new TabPage($"{multiTags.Remark}-{pageIndex++}");
                    DataGridView dgv = new DataGridView();
                    CommonHelper.CreateTextGridColumn(dgv, "PLCAddress", "PLC地址", DataGridViewContentAlignment.MiddleCenter, 220);
                    CommonHelper.CreateTextGridColumn(dgv, "DataType", "数据类型", DataGridViewContentAlignment.MiddleCenter, 150);
                    CommonHelper.CreateTextGridColumn(dgv, "OrmName", "实体属性", DataGridViewContentAlignment.MiddleCenter, 150);
                    CommonHelper.CreateTextGridColumn(dgv, "DataLength", "数据长度", DataGridViewContentAlignment.MiddleCenter, 150);
                    if (m != null && m.Count > 0)
                    {
                        foreach (var item in m)
                        {
                            var index = dgv.Rows.Add(item);
                            var row = dgv.Rows[index];
                            row.Cells["PLCAddress"].Value = item.Tag;
                            row.Cells["DataType"].Value = item.TypeName;
                            row.Cells["OrmName"].Value = item.ORMName;
                            row.Cells["DataLength"].Value = item.len;
                        }
                    }
                    tabPage.Controls.Add(dgv);  
                    dgv.InitStyle();
                    dgv.Dock = DockStyle.Fill;
                    dgv.RowHeadersVisible = false;
                    dgv.BackgroundColor = Color.WhiteSmoke;
                    dgv.BorderStyle = BorderStyle.None;
                    dgv.AllowUserToAddRows = false;
                    dgv.AllowUserToDeleteRows = false;
                    dgv.ColumnHeadersHeight = 30;
                    dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                    dgv.ReadOnly = true;
                    dgv.EnableHeadersVisualStyles = false;
                    dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                    dgv.MultiSelect = false;
                    dgv.ColumnHeadersDefaultCellStyle.Font = new Font("微软雅黑", 14);
                    dgv.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                    dgv.DefaultCellStyle.Font = new Font("微软雅黑", 10);
                    dgv.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                    dgv.RowTemplate.Height = 30;
                    this.tabControl.Controls.Add(tabPage);
                }
            }
        }
        private void InitColumns()
        {
            CommonHelper.CreateTextGridColumn(this.dataGridViewMain, "Remark", "备注", DataGridViewContentAlignment.MiddleCenter, 150);
            CommonHelper.CreateTextGridColumn(this.dataGridViewMain, "OutType", "出站类型", DataGridViewContentAlignment.MiddleCenter, 150);
            CommonHelper.CreateTextGridColumn(this.dataGridViewMain, "TriggerAddress", "出站触发地址", DataGridViewContentAlignment.MiddleCenter, 200);
            CommonHelper.CreateTextGridColumn(this.dataGridViewMain, "ResultAddress", "出站结果地址", DataGridViewContentAlignment.MiddleCenter, 200);
            CommonHelper.CreateTextGridColumn(this.dataGridViewMain, "DoneAddress", "出站完成地址", DataGridViewContentAlignment.MiddleCenter, 200);
            this.dataGridViewMain.DoubleBufferedDataGirdView(true);
            this.dataGridViewMain.InitStyle();
        }
        private void LoadProductOuts()
        {
            this.dataGridViewMain.Rows.Clear();
            if (this.mPLCStationTag != null && this.mPLCStationTag.MultiStationTagList != null && this.mPLCStationTag.MultiStationTagList.Count > 0)
            {
                foreach (var t in this.mPLCStationTag.MultiStationTagList)
                {
                    var newIndex = this.dataGridViewMain.Rows.Add(1);
                    var newRow = this.dataGridViewMain.Rows[newIndex];
                    newRow.Cells["Remark"].Value = t.Remark;
                    newRow.Cells["OutType"].Value = (int)t.OutType;
                    newRow.Cells["TriggerAddress"].Value = t.TriggerAddress;
                    newRow.Cells["ResultAddress"].Value = t.ResultAddress;
                    newRow.Cells["DoneAddress"].Value = t.DoneAddress;
                    newRow.Tag = t;
                }
            }
            this.dataGridViewMain.ClearSelection();
            this.dataGridViewMain.SelectionChanged += this.dataGridViewMain_SelectionChanged;
        }
        private void FormProductOut_Shown(object sender, EventArgs e)
        {
        }
        private void FormProductOut_FormClosing(object sender, FormClosingEventArgs e)
        {
        }
        private void buttonImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "*.xlsx|*.xlsx";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                CommonHelper.ShowWaiting(this, "正在导入数据，请稍候......");
                string filePath = openFileDialog.FileName;
                List<MultiPLCTagParameter> tags = CommonHelper.ReadMultiStationTags(filePath, (message) => { CommonHelper.ShowWaiting(this, message); });
                if (tags != null && tags.Count > 0)
                {
                    this.mPLCStationTag.MultiStationTagList.Clear();
                    this.mPLCStationTag.MultiStationTagList.AddRange(tags);
                    this.mPLCStationTag.Save();
                    LoadProductOuts();
                    HWMessageBox.Show(this, "导入成功");
                }
                else
                {
                    HWMessageBox.Show(this, "没有合适的数据");
                }
            }
            CommonHelper.CloseWaiting();
        }
        private void buttonOutput_Click(object sender, EventArgs e)
        {
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.Filter = "*.xlsx|*.xlsx";
            DialogResult result = dialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                CommonHelper.ShowWaiting(this, "正在导出数据，请稍候......");
                try
                {
                    bool outputResult = CommonHelper.OutputMultiStationTagExcel(dialog.FileName, this.mPLCStationTag.MultiStationTagList, "出站设置", (message) => { CommonHelper.ShowWaiting(this, message); });
                    if (outputResult)
                    {
                        HWMessageBox.Show(this, "导出成功");
                        CommonHelper.OpenExcelFile(dialog.FileName);
                    }
                }
                catch (Exception ex)
                {
                    HWMessageBox.Show(this, ex.Message);
                    LogHelper.Error(ex);
                }
            }
            CommonHelper.CloseWaiting();
        }
        private void dataGridViewMain_SelectionChanged(object sender, EventArgs e)
        {
            var rows = this.dataGridViewMain.SelectedRows;
            InitSubData(null);
            if (rows != null && rows.Count > 0 && this.mPLCStationTag != null
                && this.mPLCStationTag.MultiStationTagList != null
                && this.mPLCStationTag.MultiStationTagList.Count > 0)
            {
                var row = rows[0];
                var obj = row.Tag as MultiPLCTagParameter;
                if (obj != null)
                {
                    InitSubData(obj);
                }
            }
        }
    }
}
